<?PHP

	class tpl_js {
	
		public static function db_update_core(){
			return <<<EOV
var lastUpdateTime = -1;
var updateCount = 0;
var OUTID = 1;
var DEBUG = {'outgoing':false,'incoming':false};

function x7chat_ajson(){
	var request = null;
	var callBackFunction = null;
	var OUTDATASTRING = '';

	this.set_callback = function(newCallBack){
		callBackFunction = newCallBack;
	}

	this.add_outdata = function(operationId,newOutData,idNumber){
		if(!idNumber) alert('invalid use of out data function');
		OUTDATASTRING += "&operations["+operationId+"]["+idNumber+"]=" + encodeURIComponent(newOutData);
	}

	this.send_request = function(dataOut){
		soundPlayed = false;

		if(request != null){
			//alert("Output Conflict");
			//return;
		}

		try {
			request = new XMLHttpRequest();
		}catch(ex1){
			try {
				request=new ActiveXObject("Msxml2.XMLHTTP");
			}catch(ex2){
				try {
					request = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(ex3){
					alert("Your browser is not supported, please upgrade.");
					return;
				}
			}
		}

		// Load rooms for update
		var rooms = csdb.getAll('active_rooms');
		var rstring = '';
		var nrstring = '';
		var rpasses = '';
		for(rid in rooms){
			rstring += rid+';';
			if(!rooms[rid]['initiated']){
				nrstring += rid+';';
				rooms[rid]['initiated'] = true;
				csdb.add('active_rooms',rid,rooms[rid]);
			}
			if(rooms[rid]['password']){
				rpasses += '&rp' + rid + '=' + rooms[rid]['password'];
			}
		}

		request.onreadystatechange=datain;
		outString = "./index.php?act=update&nocache=" + (new Date().getTime()) + "&lastUpdateTime=" + lastUpdateTime + "&updatecount=" + updateCount++ + OUTDATASTRING + yourSession + "&rooms=" + rstring + "&initiate=" + nrstring + rpasses;

		if(DEBUG.outgoing){
			document.getElementById('message_window').innerHTML += "<hr>"+outString;
			scrollMessageWindow(true);
		}

		request.open("GET",outString,true);
		request.send(null);
		OUTDATASTRING = '';
	}

	function datain(){
		if(request.readyState == 4){
			var INDATA = new Array();

			try {
				if(DEBUG.incoming){
					document.getElementById('message_window').innerHTML += "<hr>"+request.responseText;
					scrollMessageWindow(true);
				}
				eval(request.responseText);
			}catch(e){
				document.getElementById('message_window').innerHTML += "<div class='error_box' style='border: 1px solid red;margin: 5px;padding: 5px;'><b>A fatal error has occured</b>" + request.responseText + "</div>";
			}

			for(key in INDATA){
				INDATA[key]['lastUpdateTime'] = parseFloat(INDATA[key]['lastUpdateTime']);
				if(INDATA[key]['lastUpdateTime'] > lastUpdateTime)
					lastUpdateTime = INDATA[key]['lastUpdateTime'];

				callBackFunction(INDATA[key]);
			}

			request = null;
		}
	}

}

function csdb(){
	var datadb = new Array();
	var preHooks = new Array();
	var postHooks = new Array();
	var otherHooks = new Array();

	this.addHook = function(hookType,callback,operation,database){
		if(hookType == 'pre'){
			if(!preHooks[database]) preHooks[database] = new Array();
			preHooks[database][operation] = callback;
		}else if(hookType == 'post'){
			if(!postHooks[database]) postHooks[database] = new Array();
			postHooks[database][operation] = callback;
		}else{
			otherHooks[operation] = callback;
		}
	}

	this.operation = function(INDATA){
		if(!INDATA['database']){
			try{ otherHooks[INDATA['operation']](INDATA['data']); }catch(discard){}
		}else{
			try{ preHooks[INDATA['database']][INDATA['operation']](INDATA['data']); }catch(discard){}
			switch(INDATA['operation']){
				case 'add':
					add(INDATA['database'],INDATA['index'],INDATA['data']);
				break;
				case 'remove':
					remove(INDATA['database'],INDATA['index']);
				break;
			}
			try{ postHooks[INDATA['database']][INDATA['operation']](INDATA['data']); }catch(discard){}
		}
	}

	this.add = add;
	function add(database,index,newData){
		if(!datadb[database]) datadb[database] = new Array();
		datadb[database][index] = newData;
	}

	this.remove = remove;
	function remove(database,index){
		if(!datadb[database]) datadb[database] = new Array();
		if(datadb[database][index]) delete datadb[database][index];
	}

	this.get = function(database,index,indexField){
		if(!datadb[database]) datadb[database] = new Array();
		if(!indexField){
			if(datadb[database][index])
				return datadb[database][index];
		}else{
			for(key in datadb[database]){
				if(datadb[database][key][indexField] == index)
					return datadb[database][key];
			}
		}

		return null;
	}

	this.getAll = function(database){
		if(datadb[database])
			return datadb[database];
		else
			return new Array();
	}
}
EOV;
		}
	}
?>